//路由封装
//1. 引入路由构造函数
const Router = require('koa-router')

//2. 实例化路由对象
const router = new Router()

//引入list路由
const list = require('./list')
const login = require('./login')
const db = require('../util/db')
router.use('/login', login.routes(), login.allowedMethods())

router.redirect('/', '/home')

//3. 定义路由规则
router.get('/home', async (cxt) => {
    
    cxt.body = '主页'
})

router.get('/home/banner', async (cxt) => {

    let data = await new Promise((resolve, reject) => {
        let sql = 'select * from banner'
        return db.query(sql, (err, result) => {
            if (err) throw err
            let temp = result.map(item => {
                item.imgUrl = 'localhost:300'+ item.imgUrl
            })
            console.log(temp)
            resolve(result)
        })
    })
    cxt.body = data

})

//开启list路由
router.use('/list', list.routes(), list.allowedMethods())



module.exports = router